package com.chilli.cosima.data.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.chilli.cosima.data.BaseHibernateDAO;
import com.chilli.cosima.data.bean.TOperator;
import com.chilli.cosima.data.bean.TPower;
import com.chilli.cosima.data.bean.TPowerRole;
import com.chilli.cosima.data.bean.TRole;
import com.chilli.cosima.data.dao.UserDAO;
import com.chilli.cosima.data.jdbc.DbTools;

/**
 * 用户管理DAO实现类 <功能详细描述>
 * 
 * @author TianPeng
 * @version [版本号, 2010-8-2]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class UserDAOImpl extends BaseHibernateDAO implements UserDAO
{

	public Long createUser(TOperator user) throws RuntimeException
	{
		return (Long) createObject(user);
	}

	public void deleteUser(Long[] id)
	{
		for (Long tmpId : id)
		{
			deleteObject(getObjectById(TOperator.class, tmpId));
		}
	}

	public void updateUser(TOperator user)
	{
		updateObject(user);

	}

	@SuppressWarnings("unchecked")
	public List<TOperator> findUserAll()
	{
		return findObjectByHQL("from TOperator");
	}

	public Object findUserById(Long id)
	{
		return getObjectById(TOperator.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<TOperator> findUserByRoleId(Long roleid)
	{
		return findObjectByHQL("from TOperator where roleid=" + roleid);
	}

	@SuppressWarnings("unchecked")
	public List<TOperator> findUserByName(String username)
	{
		return findObjectByHQL("from TOperator where username='" + username
				+ "'");
	}

	@SuppressWarnings("unchecked")
	public Map findUserByPage(int current, int pageSize)
	{
		Map map = new HashMap();
		List list = findObjectByHQL(" from TOperator", new String[]
		{}, current, pageSize);

		int count = findUserAll().size();

		map.put("count", count);
		map.put("list", list);
		return map;
	}

	@SuppressWarnings("unchecked")
	public Map findRoleByPage(int current, int pageSize)
	{
		Map map = new HashMap();
		List list = findObjectByHQL(" from TRole", new String[]
		{}, current, pageSize);

		int count = findAllRoles().size();

		map.put("count", count);
		map.put("list", list);
		return map;
	}

	@SuppressWarnings("unchecked")
	public List<TRole> findAllRoles()
	{
		return findObjectByHQL("from TRole");
	}

	@SuppressWarnings("unchecked")
	public String findRoleNameById(long roleid)
	{
		List<TRole> list = findObjectByHQL("from TRole t where t.id=" + roleid);
		if (list == null || list.size() < 1)
		{
			return null;
		} else
		{
			TRole role = list.get(0);
			return role.getRolename();
		}
	}

	@SuppressWarnings("unchecked")
	public List<TPower> findAllPowers()
	{
		return findObjectByHQL("from TPower");
	}

	@SuppressWarnings("unchecked")
	public TRole findRoleByName(String rolename)
	{
		List<TRole> list = findObjectByHQL("from TRole t where t.rolename='"
				+ rolename + "'");
		if (list == null || list.size() < 1)
		{
			return null;
		} else
		{
			TRole role = list.get(0);
			return role;
		}
	}

	public Long createRole(TRole role) throws RuntimeException
	{
		return (Long) createObject(role);
	}

	public Long createPowerRole(TPowerRole powerrole)
	{
		return (Long) createObject(powerrole);
	}

	public void deleteRole(Long[] id)
	{
		for (Long tmpId : id)
		{
			deleteObject(getObjectById(TRole.class, tmpId));
		}
	}

	public void deleteRolePower(Long[] id)
	{
		for (Long tmpId : id)
		{
			StringBuilder sb = new StringBuilder();
			sb.append("delete from T_POWER_ROLE t where t.roleid=").append(
					tmpId);
			DbTools.delete(sb.toString());
		}

	}

	public void updateRole(TRole role)
	{
		updateObject(role);

	}

	@SuppressWarnings("unchecked")
	public TRole findRoleById(long roleid)
	{
		List<TRole> list = findObjectByHQL("from TRole t where t.id=" + roleid);
		if (list == null || list.size() < 1)
		{
			return null;
		} else
		{
			TRole role = list.get(0);
			return role;
		}
	}

	@SuppressWarnings("unchecked")
	public List<TPowerRole> findRolePowerByRoleId(Long roleid)
	{
		return findObjectByHQL("from TPowerRole t where t.roleid=" + roleid);
	}
}
